﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SystemDirectory.Biz;
using System.Data;
using SystemDirectory.Entity;
using System.Globalization;
namespace SystemDirectory
{
    public partial class MinorEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                lbnModal.Attributes.Add("data-toggle", "modal");
                lbnModal.Attributes.Add("onclick", "btnModalClick()");
                BOContact buContact = new BOContact();
                DataTable dtContact = buContact.Get(txtFirstName.Text, txtSurName.Text, cbxInactive.Checked);
                rptContact.DataSource = dtContact;
                rptContact.DataBind();
                //modalLookup.Visible = false;
                wrap.Visible = false;

                BOMinor boMinor = new BOMinor();
                Minor minor = new Minor();
                minor = boMinor.GetById(Convert.ToInt32(Request.QueryString["id"].ToString()));
                description.Text = minor.Description.ToString();
                isTBA.Checked = minor.IsTBA;
                isActive.Checked = minor.IsActive;
                notesAction.Text = minor.NotesAction.ToString();
                estimatedCode.Text = minor.EstimatedCost.ToString();
                actualCost.Text = minor.ActualCost.ToString();
                directorate.Text = minor.Directorate.ToString();
                manager_name.Text = minor.ContactId.ToString();
                status.Text = minor.Status.ToString();
                receiveDate.Text = FormatDate(minor.EndDate.ToString());
                authorisedDate.Text = FormatDate(minor.AuthorisedDate.ToString());
                anticipatedCompletion.Text = FormatDate(minor.AnticipatedCompletion.ToString());
                completionDate.Text = FormatDate(minor.ActualCompletionDate.ToString());
            }

        }

        protected String FormatDate(String dateTime)
        {
            String format = "M/d/yyyy hh:mm:ss tt";
            DateTime dt = DateTime.ParseExact(dateTime, format, CultureInfo.InvariantCulture);
            return dt.ToString("M/d/yyyy");
        }
        protected void saveMinor_Click(object sender, EventArgs e)
        {
            Page.Validate("vgroup");
            if (Page.IsValid)
            {
                Minor minorNew = new Minor();
                minorNew.MinorId=Convert.ToInt32(Request.QueryString["id"].ToString());
                minorNew.PrimiseId = Convert.ToInt32(Request.QueryString["id"].ToString());
                minorNew.Description = description.Text;
                minorNew.EndDate = receiveDate.Text;
                minorNew.Directorate = directorate.Text;
                minorNew.Status = "i love you";
                minorNew.IsActive = isActive.Checked;
                minorNew.ContactId = Convert.ToInt32(manager_name.Text);
                minorNew.NotesAction = notesAction.Text;
                minorNew.EstimatedCost = estimatedCode.Text;
                minorNew.IsTBA = isTBA.Checked;
                minorNew.ActualCost = actualCost.Text;
                minorNew.AuthorisedDate = authorisedDate.Text;
                minorNew.AnticipatedCompletion = anticipatedCompletion.Text;
                minorNew.ActualCompletionDate = completionDate.Text;
                BOMinor boMinor = new BOMinor();
                boMinor.UpdateMinor(minorNew);
                Response.Redirect("../../Pages/Forms/PremiseAdd.aspx");
            }
        }
        protected void cbxInactive_CheckedChanged(object sender, EventArgs e)
        {
            BOContact buContact = new BOContact();
            DataTable dtContact = buContact.Get(txtFirstName.Text, txtSurName.Text, cbxInactive.Checked);
            rptContact.DataSource = dtContact;
            rptContact.DataBind();
            wrap.Visible = true;
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BOContact buContact = new BOContact();
            DataTable dtContact = buContact.Get(txtFirstName.Text, txtSurName.Text, cbxInactive.Checked);
            rptContact.DataSource = dtContact;
            rptContact.DataBind();
            wrap.Visible = true;
        }
        protected void lbnModal_Click(object sender, EventArgs e)
        {
            wrap.Visible = true;
        }
    }
}